package com.qfedu.biz.impl;

import com.qfedu.biz.GoodsBiz;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.GoodsTypeEntity;
import com.qfedu.entity.Page;
import com.qfedu.mapper.GoodsPojoMapper;
import com.qfedu.pojo.GoodsPojo;
import com.qfedu.pojo.GoodsTypePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsBizimpl implements GoodsBiz {
    @Autowired
    private GoodsPojoMapper goodsPojoMapper;

    @Override
    public List<GoodsEntity> queryAllGoods(GoodsEntity goodsEntity) {

        List<GoodsPojo> goodsPojos = goodsPojoMapper.queryAllGoods(entityToPojo(goodsEntity));
        List<GoodsEntity> goodsEntities = pojoListToEntiyList(goodsPojos);
        return goodsEntities;
    }

    //查询商品类型
    @Override
    public List<GoodsTypeEntity> queryAllGoodsType(GoodsTypeEntity goodsTypeEntity) {
        List<GoodsTypePojo> goodsTypePojos = goodsPojoMapper.queryAllGoodsType(typeEntityToPojo(goodsTypeEntity));
        List<GoodsTypeEntity> goodsTypeEntities = typeListPojoToEntity(goodsTypePojos);
        return goodsTypeEntities;
    }

    //自定义分页查询
    @Override
    public Page queryByPage(GoodsEntity goodsEntity) {
        Page page = new Page(goodsEntity.getPageNum(), goodsEntity.getPageSize());
        int count = goodsPojoMapper.queryCount();
        page.setCount(count);

        List<GoodsPojo> goodsPojos = goodsPojoMapper.queryBypage(entityToPojo(goodsEntity));
        List<GoodsEntity> goodsEntities = pojoListToEntiyList(goodsPojos);
        page.setGoodslist(goodsEntities);
        return page;
    }


    private List<GoodsTypeEntity> typeListPojoToEntity(List<GoodsTypePojo> list) {

        List<GoodsTypeEntity> lt = null;

        if(null != list && list.size() > 0){
            lt = new ArrayList<>();
            for(GoodsTypePojo gtp : list){
                lt.add(typePojoToEntity(gtp));
            }
            return lt;
        }

        return lt;
    }

    //将GoodsPojo转成GoodsEntity
    private GoodsEntity pojoToEntity(GoodsPojo gp){

        GoodsEntity ge = new GoodsEntity(gp.getGid(),gp.getGanme(),gp.getGimage(),gp.getGprice(),gp.getGtype(),gp.getGdesc(),gp.getGdiscount(),gp.getIsdelete(),gp.getGlabel(),gp.getGsex());
        ge.setGoodsTypeEntity(typePojoToEntity(gp.getGoodsTypePojo()));
        ge.setStartindex(gp.getStartindex());
        return ge;

    }

    //商品entity转pojo ; 类型转换
    private GoodsPojo entityToPojo(GoodsEntity ge){

        if(null == ge){
            return null;
        }
        GoodsPojo gp = new GoodsPojo(ge.getGid(),ge.getGname(),ge.getGimage(),ge.getGprice(),ge.getGtype(),ge.getGdesc(),ge.getGdiscount(),ge.getIsdelete(),ge.getGlabel(),ge.getGsex());
        gp.setGoodsTypePojo(typeEntityToPojo(ge.getGoodsTypeEntity()));
        gp.setStartindex(ge.getStartindex());
        return gp;
    }

    //pojo集合转成entity集合
    private List<GoodsEntity> pojoListToEntiyList(List<GoodsPojo> list){

        if(null != list && list.size() > 0){
            List<GoodsEntity> listEntity = new ArrayList<GoodsEntity>();

            GoodsEntity ge;
            for(GoodsPojo gp : list){
                ge = pojoToEntity(gp);
                listEntity.add(ge);
            }

            return listEntity;
        }

        return null;
    }

    //把商品类别的pojo转成entity
    private GoodsTypeEntity typePojoToEntity(GoodsTypePojo gp){
        if (gp==null){
            return null;
        }
        GoodsTypeEntity gte = new GoodsTypeEntity(gp.getTid(),gp.getTname(),gp.getTdesc(),gp.getTimage());

        return gte;
    }

    //把商品类别的entity转成pojo
    private GoodsTypePojo typeEntityToPojo(GoodsTypeEntity gte){
        if (gte==null){
            return null;
        }
        GoodsTypePojo gtp = new GoodsTypePojo(gte.getTid(),gte.getTname(),gte.getTdesc(),gte.getTimage());
        return gtp;
    }
}
